package cn.miwu52.homeworkerp.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.method.ReplacementTransformationMethod;

import java.util.ArrayList;
import java.util.List;

import cn.miwu52.homeworkerp.Util.MySqlOpenHelper;
import cn.miwu52.homeworkerp.bean.Product;

public class ProductDao {
    private SQLiteDatabase db;
    private MySqlOpenHelper mySqlOpenHelper;

    public ProductDao(Context context) {
        try {
            mySqlOpenHelper = new MySqlOpenHelper(context, "erp", null, context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        db = mySqlOpenHelper.getWritableDatabase();
    }

    /**
     * 根据商品名模糊查询商品
     *
     * @param name 商品名
     * @return 返回匹配的商品列表
     */
    @SuppressLint("Range")
    public List<Product> findProductByName(String name) {
        Cursor cursor = db.rawQuery("select * from Product where name like ?", new String[]{"%" + name + "%"});
        ArrayList<Product> products = new ArrayList<>();
        while (cursor.moveToNext()) {
            String productName = cursor.getString(cursor.getColumnIndex("name"));
            String id = cursor.getString(cursor.getColumnIndex("id"));
            Integer sumInEntrepot = cursor.getInt(cursor.getColumnIndex("sumInEntrepot"));
            Integer sumOutEntrepot = cursor.getInt(cursor.getColumnIndex("sumOutEntrepot"));
            Integer inEntrepot = cursor.getInt(cursor.getColumnIndex("inEntrepot"));
            Integer entrepotId = cursor.getInt(cursor.getColumnIndex("entrepotId"));
            String imageUrl = cursor.getString(cursor.getColumnIndex("imageUrl"));
            Product product = new Product(id, productName, sumInEntrepot, sumOutEntrepot, inEntrepot, entrepotId, imageUrl);
            products.add(product);
        }
        return products;
    }

    /**
     * 新增商品
     *
     * @param product 商品对象
     * @return 新增成功返回真，新增失败返回假
     */
    public boolean insertProduct(Product product) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("id", product.getId());
        contentValues.put("name", product.getName());
        contentValues.put("entrepotId", product.getEntrepotId());
        contentValues.put("imageUrl", product.getImageUrl());
        long product1 = db.insert("Product", null, contentValues);
        if (product1 >= 0) {
            return true;
        }
        return false;
    }

    /**
     * 根据ID修改商品信息
     *
     * @param product 商品对象
     * @return 修改成功返回真，修改失败返回假
     */
    public boolean updateProductById(Product product) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", product.getName());
        contentValues.put("entrepotId", product.getEntrepotId());
        int product1 = db.update("Product", contentValues, "id=?", new String[]{product.getId() + ""});
        if (product1 >= 0) {
            return true;
        }
        return false;
    }

    /**
     * 根据ID查询商品信息
     *
     * @param id 商品ID
     * @return
     */
    @SuppressLint("Range")
    public Product findProductById(String id) {
        Cursor product = db.query("Product", null, "id=" + id, null, null, null, null);
        Product product1 = new Product();
        while (product.moveToNext()) {
            product1.setId(id);
            product1.setName(product.getString(product.getColumnIndex("name")));
            product1.setSumInEntrepot(product.getInt(product.getColumnIndex("sumInEntrepot")));
            product1.setSumOutEntrepot(product.getInt(product.getColumnIndex("sumOutEntrepot")));
            product1.setInEntrepot(product.getInt(product.getColumnIndex("inEntrepot")));
            product1.setEntrepotId(product.getInt(product.getColumnIndex("entrepotId")));
        }
        return product1;
    }

    /**
     * 商品出库
     *
     * @param id 商品id
     * @return
     */
    public boolean updateProductCheckOut(String id) {
        ContentValues contentValues = new ContentValues();
        Product productById = findProductById(id);
        contentValues.put("sumOutEntrepot", productById.getSumOutEntrepot() + 1);
        contentValues.put("inEntrepot", productById.getInEntrepot() - 1);
        int product = db.update("Product", contentValues, "id=?", new String[]{id + ""});
        if (product >= 0) {
            return true;
        }
        return false;
    }

    /**
     * 商品入库
     *
     * @param id 商品ID
     * @return
     */
    public boolean updateProductCheckIn(String id) {
        ContentValues contentValues = new ContentValues();
        Product productById = findProductById(id);
        contentValues.put("sumInEntrepot", productById.getSumInEntrepot() + 1);
        contentValues.put("inEntrepot", productById.getInEntrepot() + 1);
        int product = db.update("Product", contentValues, "id=?", new String[]{id + ""});
        if (product >= 0) {
            return true;
        }
        return false;
    }

    /**
     * 根据仓库ID查询
     *
     * @param entrepotId
     * @return
     */
    @SuppressLint("Range")
    public List<Product> findProductByEntrepotId(Integer entrepotId) {
        System.out.println(entrepotId);
        Cursor cursor = db.rawQuery("select * from Product where entrepotId=?", new String[]{entrepotId + ""});
        ArrayList<Product> products = new ArrayList<>();
        if (cursor.getColumnCount() > 0) {

            while (cursor.moveToNext()) {
                String productName = cursor.getString(cursor.getColumnIndex("name"));
                String id = cursor.getString(cursor.getColumnIndex("id"));
                Integer sumInEntrepot = cursor.getInt(cursor.getColumnIndex("sumInEntrepot"));
                Integer sumOutEntrepot = cursor.getInt(cursor.getColumnIndex("sumOutEntrepot"));
                Integer inEntrepot = cursor.getInt(cursor.getColumnIndex("inEntrepot"));
                Integer newEntrepotId = cursor.getInt(cursor.getColumnIndex("entrepotId"));
                String imageUrl = cursor.getString(cursor.getColumnIndex("imageUrl"));
                Product product = new Product(id, productName, sumInEntrepot, sumOutEntrepot, inEntrepot, newEntrepotId, imageUrl);
                products.add(product);
            }
        }
        return products;
    }

    /**
     * 根据ID判断商品是否存在
     *
     * @return 返回真不存在，返回假存在
     */
    public boolean isProductById(String id) {
        Cursor product = db.query("Product", null, "id=" + id, null, null, null, null);
        if (product.getCount() > 0) {
            return false;
        }
        return true;
    }

    /**
     * 根据ID获取商品数量
     *
     * @param id
     * @return
     */
    @SuppressLint("Range")
    public Integer findProductSumById(String id) {
        Cursor product = db.query("Product", null, "id=" + id, null, null, null, null);
        while (product.moveToNext()) {
            return product.getInt(product.getColumnIndex("inEntrepot"));
        }
        return 0;
    }

    /**
     * 根据商品ID和仓库ID判断是否匹配
     *
     * @param productId  商品ID
     * @param entrepotId 仓库ID
     * @return 返回真为匹配，返回假为不存在
     */
    @SuppressLint("Range")
    public boolean isProductByEntrepot(String productId, String entrepotId) {
        Cursor product = db.query("Product", null, "id=" + productId, null, null, null, null);
        while (product.moveToNext()) {
            if (product.getString(product.getColumnIndex("entrepotId")).equals(entrepotId)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据ID修改商品名
     *
     * @param name
     * @return
     */
    public boolean changeProductNameById(String name, String id) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);
        int product = db.update("Product", contentValues, "id=?", new String[]{id});
        if (product > 0) {
            return true;
        }
        return false;
    }

    /**
     * 根据商品ID删除商品
     *
     * @param id
     * @return
     */
    public boolean deleteProductById(String id) {
        int product = db.delete("Product", "id=?", new String[]{id});
        if (product > 0) {
            return true;
        }
        return false;
    }
}
